package com.cuit.blog.service;
import com.cuit.blog.dao.ArticletypeRespository;
import com.cuit.blog.entity.Articletype;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Transactional
public class ArticletypeService {
    @Autowired
    ArticletypeRespository articletypeRespository;
    public List<Articletype> findAll()  {
	return articletypeRespository.findAll();
    }
    public Page<Articletype> findAllByPage(int pageIndex, int pageSize) {
	Sort sort = Sort.by(Sort.Direction.DESC, "id");
	Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
	Page<Articletype> articletypePage = articletypeRespository.findAll(pageable);
	return articletypePage;
    }
    public Page<Articletype> getArticletypePageByType(String name, int pageIndex, int pageSize) {
	Sort sort = Sort.by(Sort.Direction.ASC, "id");
	Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
	Page<Articletype> articletypePage = articletypeRespository.getArticletypePageByName(name, pageable);
	return articletypePage;
    }
    public List<Articletype> findAllDesc(String type) {
	return articletypeRespository.findByTypeLikeOrderByCreateTimeDesc(type);
    }
    public List<Articletype> findByTypeLike(String type) {
	return articletypeRespository.findByTypeLike(type);
    }
    public List<Articletype> findByType(String type) {
	return articletypeRespository.findByType(type);
    }
    public Articletype add(Articletype articletype) {
	return articletypeRespository.save(articletype);
    }
    public Articletype update(Articletype articletype) {
	return articletypeRespository.save(articletype);
    }
    public Articletype findById(long id) {
	return articletypeRespository.findById(id).orElse(new Articletype());
    }
    public void deleteById(long id) {
	articletypeRespository.deleteById(id);
    }
    public void updateById(String type, long id) {
	articletypeRespository.updateById(type, id);
    }
}
